<?php
namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;

/**
 * 前台轮播图控制器
 *
 * @author leo.lei(346991581@qq.com)
 */
class Picture extends BasicAdmin
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'YogaArticle';

    /**
     * 列表
     */
    public function index()
    {
        // 设置页面标题
        $this->title = '轮播图列表';
        // 实例Query对象
        $db = Db::name($this->table)->order('sort desc,id desc');
        //查询新闻列表
        $db->where('is_deleted', 0);
        $db->where('type', 2);
        // 获取到所有GET参数
        $get = $this->request->get();
        // 应用搜索条件 - 模糊搜索
        foreach (['title'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                $db->where($key, 'like', "%{$get[$key]}%");
            }
        }
        // 实例化并显示
        return parent::_list($db);
    }

    /**
     * 添加文章
     * @return View
     */
    public function add()
    {
        if ($this->request->isGet()) {
            $this->assign('title', '添加图片');
            $this->assign('cates', Db::name('YogaCategory')->where(['type'=>0,'is_deleted'=>'0'])->field('id as value,title as name')->select());
            $this->assign('vo', ['title'=>null,'author'=>null,'cid'=>null,'type'=>2,'sort'=>0,'pic'=>null,'desc'=>null,'content'=>null]);
            return view('form');
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['create_time'] = $data['update_time'] = NOW_TIME;
            if (DataService::save($this->table, $data, 'id') !== false) {
                $this->success('添加成功！', '');
            }
            $this->error('添加失败，请稍候再试！');
        }
    }

    /**
     * 编辑文章内容
     * @return View
     */
    public function edit()
    {
        $id = $this->request->get('id', '');
        if ($this->request->isGet()) {
            empty($id) && $this->error('参数错误，请稍候再试！');
            $this->assign('title', '更换图片');
            $this->assign('cates', Db::name('YogaCategory')->where(['type'=>0,'is_deleted'=>'0'])->field('id as value,title as name')->select());
            $this->assign('vo', Db::name($this->table)->where('id', $id)->find());
            return view('form');
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['update_time'] = NOW_TIME;
            if (DataService::save($this->table, $data, 'id') !== false) {
                $this->success('编辑成功！', '');
            }
            $this->error('更新失败，请稍候再试！');
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * 禁用
     */
    public function forbid()
    {
        if (DataService::update($this->table)) {
            $this->success("禁用成功！", '');
        }
        $this->error("禁用失败，请稍候再试！");
    }

    /**
     * 启用
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("启用成功！", '');
        }
        $this->error("启用失败，请稍候再试！");
    }
}
